<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>场次管理 - 管理员后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .admin-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem 0;
        }
        .showtime-card {
            border: none;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 1rem;
            transition: transform 0.2s;
        }
        .showtime-card:hover {
            transform: translateY(-2px);
        }
        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 500;
        }
        .status-selling { background-color: #d4edda; color: #155724; }
        .status-scheduled { background-color: #fff3cd; color: #856404; }
        .status-sold-out { background-color: #f8d7da; color: #721c24; }
        .status-cancelled { background-color: #f1f3f4; color: #6c757d; }
        .status-ended { background-color: #e2e3e5; color: #495057; }
        .btn-action {
            margin: 0.2rem 0;
            font-size: 0.875rem;
        }
        .movie-poster {
            width: 60px;
            height: 80px;
            object-fit: cover;
            border-radius: 5px;
        }
    </style>
</head>
<body class="bg-light">
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
        <div class="container">
            <a class="navbar-brand fw-bold" href="${pageContext.request.contextPath}/admin/">
                <i class="fas fa-cog me-2"></i>管理员后台
            </a>

            <div class="navbar-nav ms-auto">
                <!-- 主要管理功能 -->
                <a class="nav-link" href="${pageContext.request.contextPath}/admin/movies">
                    <i class="fas fa-film me-1"></i>电影管理
                </a>
                <a class="nav-link" href="${pageContext.request.contextPath}/admin/showtimes">
                    <i class="fas fa-calendar-alt me-1"></i>场次管理
                </a>
                <a class="nav-link" href="${pageContext.request.contextPath}/movies">
                    <i class="fas fa-home me-1"></i>前台首页
                </a>

                <!-- 用户菜单 -->
                <div class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user me-1"></i>${sessionScope.user.username}
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/admin/statistics">
                            <i class="fas fa-chart-bar me-2"></i>统计报表
                        </a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/logout">
                            <i class="fas fa-sign-out-alt me-2"></i>退出登录
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 管理员头部 -->
    <section class="admin-header">
        <div class="container">
            <h2><i class="fas fa-calendar-alt me-2"></i>场次管理</h2>
            <p class="mb-0">管理系统中的所有电影场次信息</p>
        </div>
    </section>

    <div class="container mt-4">
        <!-- 成功消息 -->
        <c:if test="${not empty message}">
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="fas fa-check-circle me-2"></i>${message}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </c:if>

        <!-- 错误信息 -->
        <c:if test="${not empty error}">
            <div class="alert alert-danger" role="alert">
                <i class="fas fa-exclamation-triangle me-2"></i>${error}
            </div>
        </c:if>

        <!-- 页面标题和过滤信息 -->
        <c:if test="${not empty selectedMovie}">
            <div class="alert alert-info mb-4">
                <div class="row align-items-center">
                    <div class="col-md-8">
                        <h6 class="mb-1"><i class="fas fa-filter me-2"></i>当前显示</h6>
                        <span>正在显示电影《${selectedMovie.title}》的所有场次</span>
                    </div>
                    <div class="col-md-4 text-end">
                        <a href="${pageContext.request.contextPath}/admin/showtimes" class="btn btn-sm btn-outline-primary">
                            <i class="fas fa-list me-1"></i>查看所有场次
                        </a>
                    </div>
                </div>
            </div>
        </c:if>

        <!-- 操作按钮 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h4>场次列表</h4>
            <div class="btn-group" role="group">
                <a href="${pageContext.request.contextPath}/admin/showtimes?action=add" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>添加场次
                </a>
                <a href="${pageContext.request.contextPath}/admin/movies" class="btn btn-outline-secondary">
                    <i class="fas fa-film me-2"></i>电影管理
                </a>
            </div>
        </div>

        <!-- 场次列表 -->
        <c:if test="${empty showtimes}">
            <div class="text-center py-5">
                <i class="fas fa-calendar-alt fa-3x text-muted mb-3"></i>
                <h5>暂无场次</h5>
                <p class="text-muted">点击上方按钮添加第一个场次</p>
            </div>
        </c:if>

        <c:forEach var="showtime" items="${showtimes}">
            <div class="card showtime-card">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-md-1">
                            <img src="${showtime.movie.posterUrl != null ? showtime.movie.posterUrl : '/images/default-poster.jpg'}"
                                 class="movie-poster" alt="${showtime.movie.title}">
                        </div>
                        <div class="col-md-5">
                            <h6 class="mb-1">${showtime.movie.title}</h6>
                            <div class="mt-2">
                                <small class="text-muted">
                                    <i class="fas fa-building me-1"></i>影院：${showtime.cinema.name}
                                </small>
                                <br>
                                <small class="text-muted">
                                    <i class="fas fa-door-open me-1"></i>影厅：${showtime.hall.name}
                                </small>
                                <br>
                                <small class="text-muted">
                                    <i class="fas fa-calendar me-1"></i>日期：<fmt:formatDate value="${showtime.showDate}" pattern="yyyy-MM-dd"/>
                                </small>
                                <br>
                                <small class="text-muted">
                                    <i class="fas fa-clock me-1"></i>时间：<fmt:formatDate value="${showtime.showTime}" pattern="HH:mm"/>
                                </small>
                            </div>
                        </div>
                        <div class="col-md-2 text-center">
                            <div class="mb-2">
                                <strong class="text-primary">¥${showtime.price}</strong>
                            </div>
                            <small class="text-muted">
                                座位：${showtime.availableSeats}/${showtime.totalSeats}
                            </small>
                        </div>
                        <div class="col-md-2 text-center">
                            <c:choose>
                                <c:when test="${showtime.status == 'SCHEDULED'}">
                                    <span class="status-badge status-scheduled">已安排</span>
                                </c:when>
                                <c:when test="${showtime.status == 'SELLING'}">
                                    <span class="status-badge status-selling">售票中</span>
                                </c:when>
                                <c:when test="${showtime.status == 'SOLD_OUT'}">
                                    <span class="status-badge status-sold-out">已售罄</span>
                                </c:when>
                                <c:when test="${showtime.status == 'CANCELLED'}">
                                    <span class="status-badge status-cancelled">已取消</span>
                                </c:when>
                                <c:when test="${showtime.status == 'ENDED'}">
                                    <span class="status-badge status-ended">已结束</span>
                                </c:when>
                            </c:choose>
                        </div>
                        <div class="col-md-2 text-end">
                            <div class="btn-group-vertical" role="group">
                                <!-- 编辑按钮 -->
                                <a href="${pageContext.request.contextPath}/admin/showtimes?action=edit&id=${showtime.id}"
                                   class="btn btn-outline-primary btn-action">
                                    <i class="fas fa-edit me-1"></i>编辑
                                </a>

                                <!-- 删除按钮 -->
                                <button type="button" class="btn btn-outline-danger btn-action"
                                        onclick="deleteShowtime(${showtime.id})">
                                    <i class="fas fa-trash me-1"></i>删除
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </c:forEach>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function deleteShowtime(showtimeId) {
            if (confirm('确定要删除这个场次吗？删除后无法恢复！')) {
                window.location.href = '${pageContext.request.contextPath}/admin/showtimes?action=delete&id=' + showtimeId;
            }
        }
    </script>
</body>
</html>
